package com.example.task;

import com.example.service.CustRecordsArchiveService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @description:
 * @Author: whopxx
 * @CreateTime: 2025-02-09
 */
@Component
@Slf4j
public class DataArchivingTask {

    @Resource
    private CustRecordsArchiveService custRecordsArchiveService;

    @Scheduled(cron = "0 0 0 * * ?") // 每天0点执行
//    @Scheduled(cron = "0 25 17 * * ?")
    public void archiveOldRecords() {
        log.info("开始归档营销记录...");
        // 计算31天前的日期
        LocalDateTime thresholdDate = LocalDateTime.now().minusDays(31);

        // 分页参数（每批处理10000条）
        int pageSize = 10000;
        Integer lastId = null;

        // 记录任务开始时间
        long startTime = System.currentTimeMillis();
        // 计算任务应结束的时间（20分钟后）
        long endTime = startTime + 20 * 60 * 1000;

        while (System.currentTimeMillis() < endTime) {
            lastId = custRecordsArchiveService.archiveOldRecords(pageSize, thresholdDate, lastId);
            if (lastId == null) {
                break;
            }
            log.info("最后一次归档id{}", lastId);
        }
        log.info("归档完成");
    }
}
